package com.xxx.dorm.service.impl;

import com.xxx.dorm.dao.DormManagerDao;
import com.xxx.dorm.dao.impl.DormManagerDaoImpl;
import com.xxx.dorm.domain.*;
import com.xxx.dorm.service.DormManagerService;

import java.util.List;

public class DormManagerServiceImpl implements DormManagerService {

    private DormManagerDao dormManagerDao = new DormManagerDaoImpl();

    @Override
    public List<Manage> findAll() {
        List<Manage> manages = dormManagerDao.findManager();
        return manages;
    }

    @Override
    public Integer addManager(Manage manage) {
       //调用dao完成保存宿管
        return dormManagerDao.addManager(manage);
    }

    @Override
    public void addManagerToBuild(Integer mid, String[] dormBuildIds) {
        //调用dao保存中间表信息 tb_manage_dormBuildId
        for(String buildId : dormBuildIds){
            dormManagerDao.addManagerToBuild(mid,Integer.valueOf(buildId));
        }
    }

    @Override
    public Manage findById(Integer id) {
        return dormManagerDao.findById(id);
    }

    @Override
    public void updateManager(Manage manage, String[] dormBuildIds) {
        dormManagerDao.updateManager(manage);
        dormManagerDao.deleteMiddleware(manage.getId());
        for (String buildId : dormBuildIds){
            dormManagerDao.addManagerToBuild(manage.getId(),Integer.valueOf(buildId));
        }
    }

    /**
     * 通过员工id查找管理的宿舍
     * @param id
     * @return
     */
    @Override
    public List<Building> findBuildBymid(Integer id) {
        return dormManagerDao.findBuildBymid(id);
    }

    @Override
    public void deleteManager(String id) {
        dormManagerDao.deleteManager(Integer.valueOf(id));
        dormManagerDao.deleteMiddleware(Integer.valueOf(id));
    }

    @Override
    public List<Manage> find(String conditions, String information) {
        return dormManagerDao.find(conditions,information);
    }
}
